package netprogram;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @description:
 * @author: jhy
 * @time: 2021/4/17 13:58
 */


//InetAddress类的基本方法
public class InetAddressDemo {

    public static void main(String[] args) throws UnknownHostException {

        //1.getLocalHost()--->拿到主机的InetAddress对象(包括主机名和主机的ip地址)
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);//DESKTOP-PLR7R6S/172.24.17.24

        //2.getByName()--->根据主机名拿到InetAddress对象
        InetAddress byName = InetAddress.getByName("DESKTOP-PLR7R6S");
        System.out.println(byName);//DESKTOP-PLR7R6S/172.24.17.24

        //3.getByName()--->可以根据域名拿到InetAddress对象
        InetAddress byName1 = InetAddress.getByName("www.baidu.com");
        System.out.println(byName1);//www.baidu.com/182.61.200.6

        //由于InetAddress对象包含主机名(域名)和ip，因此可以通过InetAddress对象拿到主机名(域名)和ip
        //getHostAddress()--->获取ip地址
        String hostAddress = byName1.getHostAddress();
        System.out.println("byName1的ip地址" + hostAddress);//byName1的ip地址182.61.200.6

        //getHostName()--->获取主机名/域名
        String hostName = byName1.getHostName();
        System.out.println("byName1的域名" + hostName);//byName1的域名www.baidu.com

    }
}
